﻿using System;
using System.Collections.Generic;

namespace HlslCompiler
{
    /// <summary>
    /// Factory class for version specific compilers.
    /// </summary>
    public class Compiler
    {


        /// <summary>
        /// Compiles the source code using the compiler settings extracted from the source header.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<Feedback> Compile(string source)
        {
            try
            {
                var settings = CompilerSettingsParser.GetCompilerSettings(source);
                if (settings == null)
                    return new List<Feedback>()
                               {
                                   new Feedback()
                                       {
                                           Message = "The Effect file has no compiler settings. Set Compiler settings from the Tools menu."
                                       }
                               };
                return Compile(source, settings);
            }
            catch (Exception ex)
            {
                return new List<Feedback>() { new Feedback() { Message = ex.Message } };
            }
        }

        /// <summary>
        /// Compiles the source code using the given compiler settings.
        /// </summary>
        /// <param name="source"> </param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static List<Feedback> Compile(string source, CompilerSettings settings)
        {
            try
            {
                return GetCompiler(settings).Compile(source, settings);
            }
            catch (Exception e)
            {
                return new List<Feedback>() { new Feedback() { Message = e.Message}};
            }
        }

        /// <summary>
        /// Returns the specific compiler for the requested Direct3D version.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        private static ICompiler GetCompiler(CompilerSettings settings)
        {
            switch (settings.Direct3D)
            {
                case "9":
                    return new Compiler09();
                case "10": 
                case "10.1":
                    return new Compiler10();
                case "11":
                    return new Compiler11();
                default:
                    throw new ArgumentException(string.Format("Direct3D version {0} is not supported.", settings.Direct3D));
            }
        }

    }


}
